<?php
class Default_Model_Comment extends Zend_Db_Table{

	protected $_name = COMMENT_TABLE;
	protected $_primary = 'comment_id';

	private $COMMENT_ID = 'comment_id';
	private $PHOTO_ID = 'photo_id';
	private $USER_ID = 'user_id';
	private $LOCATION_ID = 'location_id';

	protected $db;

	public function __construct(){
		//Goi ket noi voi Zend Db
		$this->db = Zend_Registry::get('connectDB');
	}

	protected $_messageError = null;

	public function countComment(){
		$select = $this->db->select()
		->from(COMMENT_TABLE,array('COUNT(comment_id) AS totalComment'));
		$result = $this->db->fetchOne($select);
		return $result;
	}
	
	public function getCommentInfoById($cmtId) {
		$select = $this->db->select()->from(COMMENT_TABLE)->where($this->COMMENT_ID.' = ?',$cmtId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}
	
	public function getCommentByPhotoId($photoId) {
		$select = $this->db->select()->from(COMMENT_TABLE)->where($this->PHOTO_ID.' = ?',$photoId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}
	
	public function getCommentByLocationId($locId) {
		$select = $this->db->select()->from(COMMENT_TABLE)->where($this->LOCATION_ID.' = ?',$locId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}
	
	public function getCommentByUserId($userId) {
		$select = $this->db->select()->from(COMMENT_TABLE)->where($this->USER_ID.' = ?',$userId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}

	// delete list comment by array of id
	public function deleteListComment($arrId) {
		try {
			$this->db->delete(COMMENT_TABLE, array($this->COMMENT_ID.' IN (?)' => $arrId));
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}
	
	// Xoa comment
	public function deleteCommentById($cmtId){
		try {
			$this->db->delete(COMMENT_TABLE,$this->COMMENT_ID.' = ' . $cmtId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}
	
	public function deleteCommentByPhotoId($photoId){
		try {
			$this->db->delete(COMMENT_TABLE,$this->PHOTO_ID.' = ' . $photoId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}
	
	public function deleteCommentByLocationId($locId){
		try {
			$this->db->delete(COMMENT_TABLE,$this->LOCATION_ID.' = ' . $locId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}
	
	public function deleteCommentByUserId($userId){
		try {
			$this->db->delete(COMMENT_TABLE,$this->USER_ID.' = ' . $userId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}

	public function getError(){
		return $this->_messageError;
	}
}